package com.hongkzh.www.other.utils;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.hongkzh.www.R;
import com.hongkzh.www.view.activity.BaseActivity;
import com.hongkzh.www.view.fragment.BaseFragment;

/**
 * 
 * @ClassName: TitleManager
 * @Description: 封装项目的title
 * @author Sandy
 * @date 2018-4-4
 * 
 */
public class TitleManager {

	protected BaseActivity mBase;
	protected BaseFragment mBaseFragment;

	public TitleManager(BaseActivity baseAty) {
		this.mBase = baseAty;
	}

	public TitleManager(BaseFragment baseFragment) {
		this.mBaseFragment = baseFragment;
	}

	/** 获取左侧按钮引用 **/
	public Button getTitleLeft() {
		// 如果activity==null 则是fragment
		return mBase == null ? (Button) mBaseFragment
				.findViewById(R.id.Btn_titleLeft) : (Button) mBase
				.findViewById(R.id.Btn_titleLeft);
	}

	/** 获取app标题容器 **/
	public RelativeLayout getAppTitleContainer() {
		return mBase == null ? (RelativeLayout) mBaseFragment
				.findViewById(R.id.rl_titleBar) : (RelativeLayout) mBase
				.findViewById(R.id.rl_titleBar);
	}

	/*** 设置标题的背景色 ***/
	public TitleManager setBackgroundResource(int resid) {
		RelativeLayout mContainer = getAppTitleContainer();
		mContainer.setBackgroundResource(resid);
		return this;
	}

	/*** 获取左侧按钮的容器 ***/
	public RelativeLayout getTitleLeftContainer() {
		return mBase == null ? (RelativeLayout) mBaseFragment
				.findViewById(R.id._title_left_container)
				: (RelativeLayout) mBase
						.findViewById(R.id._title_left_container);
	}

	/*** 设置左侧按钮图标 ****/
	public TitleManager setLeftIcon(int resid) {
		RelativeLayout titleBtnCon = getTitleLeftContainer();
		Button titleBtn = getTitleLeft();
		if (null != mBaseFragment)
			titleBtnCon.setOnClickListener(mBaseFragment);
		// 设置图标的时候设置显示
			titleBtnCon.setVisibility(View.VISIBLE);
		titleBtn.setBackgroundResource(resid);
		return this;
	}
	
	/**  设置左侧按钮为文字  **/
	public TitleManager setLeftText(CharSequence text){
		RelativeLayout  titleBtnConLayout=getTitleLeftContainer();
		Button titleLeftBtn=getTitleLeft();
		if(null!=mBaseFragment)
//			titleBtnConLayout.setOnClickListener(mBaseFragment);
			titleBtnConLayout.setVisibility(View.VISIBLE);
		titleLeftBtn.setText(text);
		return this;
	}

	/** 隐藏按钮 ***/
	public TitleManager hideLeft() {
		getTitleLeftContainer().setVisibility(View.INVISIBLE);
		return this;
	}

	/*** 获取Title 引用 ***/
	public Button getTitle() {
		return mBase == null ? (Button) mBaseFragment
				.findViewById(R.id.Btn_titleMidContent) : (Button) mBase
				.findViewById(R.id.Btn_titleMidContent);
	}

	/*** 获取Title里Incon的引用 ***/
	public ImageView getTitleIcon() {
		return mBase == null ? (ImageView) mBaseFragment
				.findViewById(R.id.iv_titleIcon) : (ImageView) mBase
				.findViewById(R.id.iv_titleIcon);
	}

	
	/*** 获取 title的父容器 ***/
	public RelativeLayout getTitleContainer() {
		return mBase == null ? (RelativeLayout) mBaseFragment
				.findViewById(R.id.rl_titleMid) : (RelativeLayout) mBase
				.findViewById(R.id.rl_titleMid);
	}

	/*** 设置标题的内容 ***/
	public TitleManager setTitle(CharSequence text) {
		RelativeLayout titleContainer = getTitleContainer();
		// 先显示设置
		Button titleBtn = getTitle();
		if (null != mBaseFragment)
			titleContainer.setOnClickListener(mBaseFragment);
		titleContainer.setVisibility(View.VISIBLE);
		titleBtn.setText(text);
		return this;
	}

	/**设置标题的图标**/
	public TitleManager setTitleIcon(int resId) {
		RelativeLayout titleBtnContainer = getTitleContainer();
		ImageView titleIcon=getTitleIcon();
		if(null!=mBaseFragment)
			titleBtnContainer.setOnClickListener(mBaseFragment);
		titleBtnContainer.setVisibility(View.VISIBLE);
		titleIcon.setVisibility(View.VISIBLE);
		titleIcon.setBackgroundResource(resId);
		return this;
	}

	
	/*** 获取右侧按钮的容器 ***/
	public RelativeLayout getTitleRightContainer() {
		return mBase == null ? (RelativeLayout) mBaseFragment
				.findViewById(R.id._title_right_container)
				: (RelativeLayout) mBase
						.findViewById(R.id._title_right_container);
	}
	
	/** 获取右侧按钮引用 **/
	public Button getTitleRight() {
		// 如果activity==null 则是fragment
		return mBase == null ? (Button) mBaseFragment
				.findViewById(R.id.Btn_titleRight) : (Button) mBase
				.findViewById(R.id.Btn_titleRight);
	}
	

	/*** 设置右侧按钮图标 ****/
	public TitleManager setRightIcon(int resid) {
		RelativeLayout titleBtnCon = getTitleRightContainer();
		Button titleBtn = getTitleRight();
		if (null != mBaseFragment)
			titleBtnCon.setOnClickListener(mBaseFragment);
		// 设置图标的时候设置显示
		titleBtnCon.setVisibility(View.VISIBLE);
		titleBtn.setBackgroundResource(resid);
		return this;
	}
	
	/**  设置右侧按钮为文字  **/
	public TitleManager setRightText(CharSequence text){
		RelativeLayout  titleBtnConLayout=getTitleRightContainer();
		Button titleRightBtn=getTitleRight();
		if(null!=mBaseFragment)
			titleBtnConLayout.setOnClickListener(mBaseFragment);
		titleBtnConLayout.setVisibility(View.VISIBLE);
		titleRightBtn.setText(text);
		return this;
	}

	/*** 隐藏右侧按钮 ***/
	public TitleManager hideRight(){
		getTitleRightContainer().setVisibility(View.GONE);
		return this;
	}
	
}
